// utils/api.js - 模拟数据版本
class ApiClient {
  // 模拟延迟
  static delay(ms = 500) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  // 模拟用户数据
  static mockUserData = {
    id: 1,
    username: '学霸小明',
    avatar: '/static/icons/avatar-placeholder.png',
    level: '专注达人',
    total_study_time: 3600, // 分钟
    streak_days: 15,
    points: 1250,
    badges: [
      { name: '早起鸟', icon: '/static/icons/points.png', description: '连续7天早上学习' },
      { name: '夜猫子', icon: '/static/icons/points.png', description: '连续7天晚上学习' },
      { name: '马拉松', icon: '/static/icons/points.png', description: '单次学习超过3小时' }
    ]
  };

  // 模拟学习会话数据
  static mockStudySessions = [
    {
      id: 1,
      planned_start_time: '2024-01-15T09:00:00Z',
      planned_end_time: '2024-01-15T11:00:00Z',
      planned_duration: 120,
      actual_start_time: '2024-01-15T09:05:00Z',
      actual_end_time: '2024-01-15T11:10:00Z',
      actual_duration: 125,
      status: 'completed',
      partner: {
        id: 2,
        username: '学习小红',
        avatar: '/static/icons/avatar-placeholder.png'
      }
    },
    {
      id: 2,
      planned_start_time: '2024-01-15T14:00:00Z',
      planned_end_time: '2024-01-15T16:00:00Z',
      planned_duration: 120,
      status: 'scheduled'
    }
  ];

  // 模拟匹配数据
  static mockMatches = [
    {
      id: 101,
      status: 'matched',
      partner: {
        id: 3,
        username: '考研小王',
        avatar: '/static/icons/avatar-placeholder.png',
        level: '学霸',
        subjects: ['数学', '英语']
      },
      session: {
        planned_start_time: '2024-01-15T19:00:00Z',
        planned_duration: 90
      }
    }
  ];

  // 创建学习会话
  static async createStudySession(startTime, endTime, duration) {
    await this.delay();
    const newSession = {
      id: Date.now(),
      planned_start_time: this.formatTimeForAPI(startTime),
      planned_end_time: this.formatTimeForAPI(endTime, true, startTime),
      planned_duration: Math.round(parseFloat(duration) * 60),
      status: 'scheduled',
      created_at: new Date().toISOString()
    };
    this.mockStudySessions.unshift(newSession);
    return { success: true, data: newSession };
  }

  // 智能匹配
  static async randomMatch(sessionId, message = '寻找学习伙伴') {
    await this.delay(1000);
    return {
      success: true,
      data: {
        match_id: Date.now(),
        status: 'waiting',
        estimated_wait_time: 30,
        message: '正在为您匹配合适的学习伙伴...'
      }
    };
  }

  // 获取用户信息
  static async getUserProfile() {
    await this.delay();
    return { success: true, data: this.mockUserData };
  }

  // 获取学习会话列表
  static async getStudySessions() {
    await this.delay();
    return { success: true, data: this.mockStudySessions };
  }

  // 开始学习会话
  static async startStudySession(sessionId) {
    await this.delay();
    const session = this.mockStudySessions.find(s => s.id === sessionId);
    if (session) {
      session.status = 'in_progress';
      session.actual_start_time = new Date().toISOString();
    }
    return { success: true, data: session };
  }

  // 结束学习会话
  static async endStudySession(sessionId, actualDuration, exitReason = null) {
    await this.delay();
    const session = this.mockStudySessions.find(s => s.id === sessionId);
    if (session) {
      session.status = 'completed';
      session.actual_end_time = new Date().toISOString();
      session.actual_duration = actualDuration;
      session.exit_reason = exitReason;
    }
    return { success: true, data: session };
  }

  // 取消匹配
  static async cancelMatch(matchId) {
    await this.delay();
    return { success: true, data: { message: '匹配已取消' } };
  }

  // 获取匹配详情
  static async getMatchDetail(matchId) {
    await this.delay();
    const match = this.mockMatches.find(m => m.id === matchId);
    return { success: true, data: match || this.mockMatches[0] };
  }

  // 格式化时间为API所需格式
  static formatTimeForAPI(time, isEndTime = false, startTime = null) {
    const today = new Date();
    const [hours, minutes] = time.split(':').map(Number);
    let dateTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), hours, minutes);
    
    if (isEndTime && startTime) {
      const [startHours, startMinutes] = startTime.split(':').map(Number);
      const startDateTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), startHours, startMinutes);
      
      if (dateTime <= startDateTime) {
        dateTime.setDate(dateTime.getDate() + 1);
      }
    }
    
    return dateTime.toISOString();
  }
}

module.exports = ApiClient;